﻿//Ana Isabel Ibáñez García
//version 3: class is created, attributes are created, methods: selectClass, DeselectClass, SetRelationToCreate
//AddClass
//version 4: List with all relations, added Methods:GetRelations,AddRelations, 
//changed methods: SelectClass
//version 6: changed method Selectclass to refresh the form with the data
//added method: AddAttributeToForm, ModifyAttribute
//version 7: added methods: AddMethodToForm, ModifyMethod
//version 9: added method exportToCode

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace UMLDesigner
{
    static class WindowsManager
    {
        private static MainWindow mainWindow;

        private static FormControl formControl;
        private static List<RelationView> relations = new List<RelationView>();
        private static List<ClassView> classes = new List<ClassView>();

        private static ClassView selectedClass = null;
        private static RelationView relationToCreate = null;

        public static Form Build()
        {
            mainWindow = new MainWindow();
            formControl = mainWindow.GetControlFormulario();
            return mainWindow;
           
        }

        public static List<RelationView> GetRelations()
        {
            return relations;
        }

        public static void AddRelations(RelationView rel, ClassView iniClass, ClassView endClass)
        {
            if (rel is InheritanceView)
            {
                rel.SetIniClass(iniClass);
                rel.SetEndClass(endClass);
            }
            else
            {
                rel.SetIniClass(endClass);
                rel.SetEndClass(iniClass);
            }
            relations.Add(rel);
            mainWindow.PaintRelations();
        }

        public static void SelectClass(ClassView classView)
        {
            if (relationToCreate != null && selectedClass != null)
            {
                AddRelations(relationToCreate, selectedClass, classView);
                relationToCreate = null;
            }

            DeselectClass();
            selectedClass = classView;
            selectedClass.Select();
            formControl.Refresh(classView);
        }

        public static void DeselectClass()
        {
            if (selectedClass != null)
            {
                selectedClass.Deselect();
                selectedClass = null;
            }
        }

        public static void SetRelationToCreate(RelationView newRel)
        {
            relationToCreate = newRel;
        }

        public static void AddClass(ClassView newClassView)
        {
            classes.Add(newClassView);
        }

        public static void AddAttributeToForm(string name, string type, string visibility)
        {
            formControl.AddAttribute(name, type, visibility);

        }
        public static void ModifyAttribute(string name, string type, string visibility)
        {
            formControl.ModifyAttribute(name, type, visibility);
        }

        public static void AddMethodToForm(string visibility, string name, List<ParameterData> parameters, string returnType)
        {
            formControl.AddMethods(visibility, name, parameters, returnType);
        }

        public static void ModifyMethod(string visibility, string name, List<ParameterData> parameters, string returnType)
        {
            formControl.EditMethod(visibility, name, parameters, returnType);
        }

        public static void EditRelation(string name, string iniCard, string endCard, string iniAttr, string endAttr, string iniAttrVisi, string endAttrVisi)
        {
            formControl.EditRelation(name, iniCard, endCard, iniAttr, endAttr, iniAttrVisi, endAttrVisi);

        }

        public static void PaintRelations()
        {
            mainWindow.PaintRelations();
        }

        public static void DeleteClass()
        {
            classes.Remove(selectedClass);
            mainWindow.DeleteClass(selectedClass);
            DeselectClass();
        }

        public static List<RelationshipData> GetRelationOfClass(ClassData classData)
        {
            List<RelationshipData> relationsOfClass = new List<RelationshipData>();

            foreach (RelationView rel in relations)
            {
                if (rel.GetRelationData().GetRelationType() == RelationshipData.RelationType.Inheritance)
                {
                    if (rel.GetIniClass().GetClassData() == classData)
                    {
                        relationsOfClass.Add(rel.GetRelationData());
                    }
                }
                else if (rel.GetEndClass().GetClassData() == classData)
                {
                    relationsOfClass.Add(rel.GetRelationData());
                }

            }

            return relationsOfClass;
        }

        public static void ExportToCode(string projectName, string path)
        {
            foreach (ClassView classView in classes)
            {
                String name = classView.GetClassData().GetName();
                StreamWriter fileW = File.CreateText(path + "/" + name + ".cs");
                classView.GetClassData().ExportCode(fileW, projectName);
                fileW.Close();
            }
        }

        public static List<string> GetDeclaredTypes()
        {
            List<string> classTypes = new List<string>();
            foreach (ClassView classView in classes)
            {
                classTypes.Add(classView.GetClassData().GetName());
            }
            return classTypes;
        }

    }
}
